package com.youhome.controller;

import com.github.pagehelper.PageInfo;
import com.youhome.model.BClient;
import com.youhome.model.BCounselor;
import com.youhome.model.BShareGroup;
import com.youhome.model.BShareGroupHouse;
import com.youhome.response.Result;
import com.youhome.response.ResultCode;
import com.youhome.response.ResultPage;
import com.youhome.service.CounselorService;
import com.youhome.service.ShareGroupHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * Created by chen365 on 2018/09/18.
 */
@Api(tags = "分享组房源")
@RestController
@RequestMapping(value = "/share_groups")
public class ShareGroupHouseController {

    @Autowired
    private CounselorService counselorService;
    @Autowired
    private ShareGroupHouseService shareGroupHouseService;


    @ApiOperation(value = "列表", notes = "列表", response = BShareGroup.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页码", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", required = false, paramType = "query")
    })
    @GetMapping(value = "/{shareId}/houses")
    public Object list(HttpServletRequest request, @PathVariable String shareId, Integer pageNumber, Integer pageSize) {
        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor == null) {
            return new Result<>(ResultCode.LAND_EXPIRATION);
        }

        if (StringUtils.isEmpty(shareId)) {
            return new ResultPage<>(ResultCode.ERROR_PARAMETER);
        }
        BShareGroupHouse record = new BShareGroupHouse();
        record.setShareId(shareId);
        PageInfo<BShareGroupHouse> bClientPageInfo = shareGroupHouseService.selectByBean(pageNumber, pageSize, record);
        return new ResultPage<>(ResultCode.SUCCESS, bClientPageInfo);
    }


    @ApiOperation(value = "新增", notes = "新增", response = BClient.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源Id", required = true, paramType = "query")

    })
    @PostMapping(value = "/{shareId}/houses")
    public Object add(HttpServletRequest request, @PathVariable String shareId, String houseId) {

        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor == null) {
            return new Result<>(ResultCode.LAND_EXPIRATION);
        }

        BShareGroupHouse bShareGroupHouse = new BShareGroupHouse();

        if (StringUtils.isEmpty(shareId)) {
            return new Result<>(ResultCode.ERROR_PARAMETER);
        }
        bShareGroupHouse.setShareId(shareId);

        if (StringUtils.isEmpty(houseId)) {
            return new Result<>(ResultCode.ERROR_PARAMETER);
        }
        bShareGroupHouse.setHouseId(houseId);

        int success = shareGroupHouseService.insert(bShareGroupHouse);
        if (success > 0) {
            return new Result<>(ResultCode.SUCCESS);
        }

        return new Result<>(ResultCode.FAIL);
    }

    @ApiOperation(value = "删除", notes = "删除", response = BCounselor.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "Long", name = "id", value = "id", required = true, paramType = "path"),
    })
    @DeleteMapping(value = "/{shareId}/houses/{id}")
    public Object delete(HttpServletRequest request, @PathVariable String shareId, @PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            return new Result<>(ResultCode.EMPTY_PARAMETER);
        }
        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor == null) {
            return new Result<>(ResultCode.LAND_EXPIRATION);
        }

        if (shareGroupHouseService.deleteByPrimaryKey(id) > 0) {
            return new Result<>(ResultCode.SUCCESS);
        }

        return new Result<>(ResultCode.FAIL);
    }
}
